package com.scau.yangsuiyu.estore.controller.seller.manageShop;

import com.scau.yangsuiyu.estore.controller.seller.EditGoodsPane;
import com.scau.yangsuiyu.estore.controller.myComponent.ShopGoodsHBox;
import com.scau.yangsuiyu.estore.dababases.dao.ShopDao;
import com.scau.yangsuiyu.estore.dababases.entity.Goods;
import com.scau.yangsuiyu.estore.dababases.entity.Shop;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.control.Pagination;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;

import java.io.IOException;
import java.net.URL;
import java.util.*;

/**
 * @author suiyu_yang
 * @description 商品管理子界面
 * @date 2019-06-09 17:56
 * @email suiyu_yang@163.com
 */
public class ShopGoodsManagePane extends AnchorPane implements Initializable {
    private Shop shop;
    private int itemsPerPage = 5;
    private List<HBox> hBoxList = new ArrayList<>();
    private List<Goods> goodsList;

    @FXML
    private Pagination pagination;

    @FXML
    private VBox goodsVBox;

    public ShopGoodsManagePane(Shop shop) {
        this.shop = shop;
        loadFXML();
    }

    private void loadFXML() {
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/view/seller/manageShop/ShopGoodsManage.fxml"));
        fxmlLoader.setController(this);
        fxmlLoader.setRoot(this);
        try {
            fxmlLoader.load();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        this.setOnMouseClicked(e -> {
            shop = new ShopDao().getShopById(shop.getId());
        });
        int pageNum;
        goodsList = shop.getGoodsList();
        if (goodsList.size() % itemsPerPage == 0) {
            pageNum = goodsList.size() / itemsPerPage;
        } else {
            pageNum = goodsList.size() / itemsPerPage + 1;
        }
        pagination.setPageCount(pageNum);
        pagination.setPageFactory((Integer pageIndex) -> createPage(pageIndex));

    }

    @FXML
    private void addGoodsClick() {
        EditGoodsPane editGoodsPane = new EditGoodsPane(this);
        editGoodsPane.showPane();
    }

    /**
     * 创建每一页的对象
     *
     * @param pageIndex
     * @return
     */
    private VBox createPage(int pageIndex) {
        int page = pageIndex * itemsPerPage; //当前是第几个商品
        goodsVBox = new VBox();
        if (goodsList.size() != 0) {
            for (int i = page; i < page + itemsPerPage; i++) {
                if (i < goodsList.size()) {
                    ShopGoodsHBox goodsHBox = new ShopGoodsHBox((Goods) goodsList.toArray()[i], this);
                    hBoxList.add(goodsHBox);
                    goodsVBox.getChildren().add(goodsHBox);
                }
            }
        }
        return goodsVBox;
    }

    public Shop getShop() {
        shop = new ShopDao().getShopById(shop.getId());
        return shop;
    }

    public void updateUI() {
        goodsVBox.getChildren().removeAll(hBoxList);
        int pageNum;
        goodsList = shop.getGoodsList();
        if (goodsList.size() % itemsPerPage == 0) {
            pageNum = goodsList.size() / itemsPerPage;
        } else {
            pageNum = goodsList.size() / itemsPerPage + 1;
        }
        pagination.setPageCount(pageNum);
        pagination.setPageFactory((Integer pageIndex) -> createPage(pageIndex));
    }
}
